console When your entity model is inconsistent with the database schema, the following error is thrown:The model backingthe ' schoolcontext ' context has changed since, the database was created. Consider using Code first migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269)Baidu Search Code First
[Index page][Download source code]
Refined. NET 4.0 (10)-automatically update the database structure (Automatic Migrations) under the Code First of ADO. NET Entity Framework 4.3)
Author: webabcd
IntroductionNew Feature of ADO. NET Entity Framework 4.3: automatically update the database structure (Automatic Migrations)
constructor of the class file PortalContext.cs to remove the setting that removes the current database from rebuilding the new database when the database model has changed.Static Portalcontext () { database.setinitializer1>, in the Package Manager console, execute the statement:Pm> enable-migrations-enableautomaticmigrationsAfter successful execution, add the Migrations folder to the Portal console appl
Learning ASP. net mvc (9) -- "Code First Migrations" tool example, mvcmigrations
In the previous article, we learned how to use the "Code First Migrations" tool of the Entity Framework and use the "Migration" function to modify the model class, synchronously update the table structure of the corresponding database.
In
database: 2. EF Code First Database migration 2.1. Build the Database Modify the static constructor of the class file PortalContext.cs to remove the setting that removes the current database from rebuilding the new database when the database model has changed. Static Portalcontext ()
{
database.setinitializer 1>, in the Package Manager console, execute the statement: Pm> enable-migrations-enableautoma
. Build the Database Modify the static constructor of the class file PortalContext.cs to remove the setting that removes the current database from rebuilding the new database when the database model has changed. Static Portalcontext ()
{
database.setinitializer 1>, in the Package Manager console, execute the statement: Pm> enable-migrations-enableautomaticmigrations After successful execution, add the Migrati
Learning ASP. net mvc (8) -- "Code First Migrations" tool, mvcmigrations
In this article, we will learn how to use the "Code First Migrations" (also known as the Code First feature) tool of the Entity Framework and use the "Migration" feature to modify the model class, synch
In this article, we learned how to use the Entity Framework's Code First migrations (also known as the Code -first feature) tool to make some modifications to the model class using the migration feature, while simultaneously updating the table structure of the corresponding database.By default, when you use the Entity Framework's
UserInfo Userinfo{get;set;}}Note: The difference is that we add a length limit to the TeacherName attribute. Next, we're going to start persisting this model into the database (we're just modifying the property now, the length of this field in the database is nvarchar (max), not nvarchar (10))1: Configure the database connection in config: 2: Open the NuGet console:3: Run command enable-migrationsThe following error may occur:Checking If the context targets an existing database ... Detected da
now, the length of this field in the database is nvarchar (max), not nvarchar (10))1: Configure the database connection in config:[HTML]View Plaincopyprint?
connectionstrings >
addname= "Testusersdb" connectionstring= " integratedsecurity=sspi; Persistsecurityinfo=false;initialcatalog=testusersdb;datasource=xcl-pc\sqlexpress " providername= "System.Data.SqlClient" />
connectionstrings>
2: Open the NuGet console:3: Run command enable-migrationsThe fol
1>, in the Package Manager console, execute the statement:Pm> enable-migrations-enableautomaticmigrationsAfter execution succeeds, add the Migrations folder to the application code structure, and generate the class file Configuration.cs.2>, in the Package Manager console, execute the statement:Pm> add-migration InitialcreateAfter successful execution, new class f
model changes, the current database is deleted and the new database is rebuilt.After the code executes, the resulting database: 2. EF Code First Database migration2.1. Build the DatabaseModify the static constructor of the class file PortalContext.cs to remove the setting that removes the current database from rebuilding the new database when the database model has changed. Static Portalcontext () { d
constructor of the class file PortalContext.cs to remove the setting that removes the current database from rebuilding the new database when the database model has changed.Static Portalcontext () { database.setinitializer1>, in the Package Manager console, execute the statement:Pm> enable-migrations-enableautomaticmigrationsAfter successful execution, add the Migrations folder to the Portal console appl
EF Code First Migrations database migration, efmigrations1. EF Code First creates a database
Step 1: Create a console application
Step 2: Install EntityFramework
Run the following statement on the Package Manager Console:
PM> Install-Package EntityFramework2. Project Structure
Two entities and ing. The PortalContext code
Label:Reference:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/ Adding-a-new-field-to-the-movie-model-and-table Ext.: http://www.it165.net/pro/html/201403/10653.html The content of this article: 1, learning entity Framework Code First migration feature (migrations) 2, update the Model class (Add new field in Model Class), and then apply the update to Datebase. By default, just as we
TeacherName {get; set;}
public virtual UserInfo Userinfo{get;set;}
Note: The difference is that we add a length limit to the TeacherName attribute.
Next, we'll start to persist this model to the database (we're just modifying the property now, the length of this field in the database is nvarchar (max), not nvarchar (10))
1: Configure the database connection in config:
2: Open the NuGet console:
3: Run command enable-migrations
The
= true; }
Execute Update-database again:
Because I changed the length from Max to 10, when updating the data structure, it thinks this operation will cause data loss, as shown below:
Specify the '-verbose' flag to view the SQL statements being applied to the target database.No pending code-based migrations.Applying automatic migration: 201212090848057_automaticmigration.Automatic migration was not applied because it wowould result in data loss
automatic migration instead, delete the Migrations folder and re-run enable-migrations specifying The-enableaut Omaticmigrations parameter.Code first migrations enabled for Project MvcApplication1.The following folder appears in the project:Opening the Configuation.cs will make the following changes:[CSharp]View PlainCopy
Public Configuration ()
{
After using the Codefirst, a big problem is the synchronization of the attribute fields in the code with the tables in the database, the deletion of the database regeneration can be solved, but the data is lost (of course, by initializing the database in the code to add data can also be resolved, the initialization of the task can be achieved by overriding the seed method). There are a lot of situations whe
;} /* in order to test Automatic migrations You can release this comment and add-migration The corresponding code for the structure migration will be generated automatically public double price {get; set;} */}} MyContext.csUsingSystem;UsingSystem.Collections.Generic;UsingSystem.Linq;Usingsystem.web;UsingSystem.Data.Entity;namespace EF43. updateschema{// Create a Context to inherit from DbContext pub
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.